package com.victor.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.victor.dao.StatisticsDao;
import com.victor.entity.Statistics;
import com.victor.service.StatisticsService;

@Service
public class StatisticsServiceImpl implements StatisticsService {

	@Autowired
	private StatisticsDao dao;

	@Override
	public boolean batchDelete(int[] ids) {
		return dao.batchDelete(ids) > 0 ? true : false;
	}

	@Override
	public boolean insert(Statistics record) {
		return dao.insert(record) > 0 ? true : false;
	}

	@Override
	public Statistics findById(Integer sId) {
		return dao.findById(sId);
	}

	@Override
	public boolean update(Statistics record) {
		return dao.update(record) > 0 ? true : false;
	}

	@Override
	public Statistics findByParam(String hPicId, String hUserId, String time) {
		return dao.findByParam(hPicId, hUserId, time);
	}

	@Override
	public Map<String, Object> weekReport(String userid, String pictype) {
		List<Statistics> temp = dao.findWeekReport(userid, pictype);
		Map<String, Object> map = new HashMap<>();
		map.put("readnum", temp.size());
		Integer readlength = temp.stream().collect(Collectors.summingInt(a -> Integer.valueOf(a.getSLength())));
		map.put("readlength", readlength);
		int size = temp.stream()
				.collect(Collectors.collectingAndThen(
						Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Statistics::getSTime))),
						ArrayList::new))
				.size();
		map.put("readtimesum", size);
		return map;
	}

	@Override
	public Map<String, Object> readScann(String userid, String pictype) {
		List<Map<String, String>> thisMonth = dao.findThisMonth(userid, pictype);
		List<Map<String, String>> lastMonth = dao.findLastMonth(userid, pictype);
		Map<String, Object> result = new HashMap<>();
		result.put("thisMonth", thisMonth);
		result.put("lastMonth", lastMonth);
		return result;
	}

	@Override
	public List<String> findPastMonth(String userid, String pictype) {
		return dao.findPastMonth(userid, pictype);
	}

	@Override
	public List<Map<String, Object>> batchByUserid(List<Integer> useridList, String date, String pictype) {
		return dao.batchByUserid(useridList, date, pictype);
	}

}
